﻿using UnityEngine;
using UnityEngine.UI;

public abstract class ViewBase : UIBase
{
    public ViewDefine mKey;
    public UILayer mLayer;
    public object[] _params;
    public Canvas mCanvas;
    public GraphicRaycaster mRaycaster;
    public bool isOpen;//是否打开
    public bool isShow;//是否显示
    public bool isTip;//是否遮盖
    public bool isPersistent;//是否常驻
    public float cacheTime;//缓存时间

    public void Init(ViewDefine key, GameObject go)
    {
        Init(go);
        mKey = key;
        mCanvas = mGo.AddComponent<Canvas>();
        mRaycaster = mGo.AddComponent<GraphicRaycaster>();
        isPersistent = true;
        DoLoaded();
        SetLayer();
    }

    public void SetLayer()
    {
        int layer = (int)mLayer;
        mTran.SetParent(UIManager.Instance.LayerTrans[layer]);
        mTran.localPosition = Vector3.zero;
        mTran.localScale = Vector3.one;
        mTran.SetRectOffset(0, 0, 0, 0);
    }

    public void Open(params object[] param)
    {
        isOpen = true;
        _params = param;
        SetVisible(true);
        DoOpen();
    }

    public void Close()
    {
        isOpen = false;
        SetVisible(false);
        Clear();
        DoClose();
    }

    public void Show()
    {
        SetVisible(true);
        DoShow();
    }

    public void Hide()
    {
        SetVisible(false);
        DoHide();
    }

    public void Dispose()
    {
        DoDispose();
    }

    public void SetVisible(bool visible)
    {
        isShow = visible;
        mCanvas.enabled = visible;
        mRaycaster.enabled = visible;
        if (isShow)
            mTran.SetAsLastSibling();
    }

    public T CreateItem<T>(ItemDefine key, RectTransform parent, params object[] param) where T : UIItemBase
    {
        return UIManager.Instance.CreateItem<T>(this, key, parent, param);
    }

    public abstract void DoLoaded();
    public abstract void DoOpen();
    public abstract void DoClose();
    public abstract void DoShow();
    public abstract void DoHide();
    public abstract void DoDispose();
}
